package com.px.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.px.dto.ReturnResult;
import com.px.dto.ReturnResultUtils;
import com.px.pojo.ShOrder;
import com.px.service.ShOrderService;
import com.px.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/shOrder")
public class ShOrderController {

    @Resource
    private ShOrderService shOrderService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private ObjectMapper objectMapper;

    @GetMapping("/list")
    public ReturnResult findUserByPage(@RequestParam(required = false) String orderNo,
                                       @RequestParam(required = false) Integer userId,
                                       @RequestParam(required = false) Integer commodityId,
                                       @RequestParam(required = false) Double minPrice,
                                       @RequestParam(required = false) Double maxPrice,
                                       @RequestParam(required = false) Integer payStatus,
                                       @RequestParam(required = false) String aliTradeNo,
                                       @RequestParam(required = false) String pageIndex,
                                       @CookieValue(required = false) String account,
                                       Model model)throws Exception{
        Integer currentPageNo =1;
        //获取页码
        if(!StringUtils.isEmpty(pageIndex)){
            currentPageNo = Integer.parseInt(pageIndex);
        }
        //控制页码
        if(currentPageNo<=0){
            currentPageNo = 1;
        }
        Integer pageSize = 5;
        //查询出总记录数
        Integer totalCount = shOrderService.selectShOrderCount(orderNo, userId,commodityId,minPrice,maxPrice,payStatus,aliTradeNo);
        //根据总记录数算出总页数
        Integer temp = totalCount/pageSize;
        Integer totalPageCount = totalCount%pageSize ==0? temp:temp+1;
        if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        List<ShOrder> shOrders = shOrderService.selectShOrderList(orderNo, userId, commodityId, minPrice, maxPrice, payStatus, aliTradeNo, currentPageNo, pageSize);
        model.addAttribute("shOrders",shOrders);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("orderNo",orderNo);
        model.addAttribute("userId",userId);
        return ReturnResultUtils.returnSuccess(shOrders);
    }

    @GetMapping("/view/{id}")
    public ReturnResult findUserList(@PathVariable Integer id, Model model)throws Exception{
        ShOrder shOrder = shOrderService.selectShOrder(id);
        return ReturnResultUtils.returnSuccess(shOrder);
    }

    @GetMapping("/toAdd/{id}")
    public String toAdd(@PathVariable Integer id, Model model)throws Exception{
        ShOrder shOrder = shOrderService.selectShOrder(id);
        return "sysUser/add";
    }

    @GetMapping ("/add")
    public ReturnResult addShUser(ShOrder shOrder,Model model)throws Exception{
        int insert = shOrderService.insertShOrder(shOrder);
        ShOrder shOrder1 = shOrderService.selectShOrder(shOrder.getOrderId());
        if(insert > 0){
            return ReturnResultUtils.returnSuccess(shOrder1);
        }else{
            return ReturnResultUtils.returnFail(1001,"失败");
        }
    }

    @GetMapping("/modify/{id}")
    public String toUpdate(@PathVariable Integer id, Model model)throws Exception{
        ShOrder shOrder = shOrderService.selectShOrder(id);
        return "sysUser/update";
    }

    @GetMapping ("/update")
    public ReturnResult updateShUser(ShOrder shOrder,Model model)throws Exception{
        int update = shOrderService.updateShOrder(shOrder);
        ShOrder shOrder1 = shOrderService.selectShOrder(shOrder.getOrderId());
        if(update > 0){
            return ReturnResultUtils.returnSuccess(shOrder1);
        }else{
            return ReturnResultUtils.returnFail(1001,"失败");
        }
    }

    @GetMapping("/del/{userid}")
    public ReturnResult deleteShUser(@PathVariable Integer userid,
                                     Model model)throws Exception{
        int delete = shOrderService.deleteShOrder(userid);
        Map<String,Object> map = new HashMap<>();
        if(delete > 0){
            return ReturnResultUtils.returnSuccess();
        }else{
            return ReturnResultUtils.returnFail(1001,"失败");
        }
    }
}
